package com.snowy.hidden.util;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class CustomUtil {
//    public static void main(String[] args) {
//        CustomUtil.FromBase64("eyJnYXRlSXAiOiIxMC44LjExNy4yNTEiLCJnYXRlV2F5IjoiMTAuOC4xMTcuMSIsIm1hY0FkZHJlc3MiOiJFODo5Qjo5RTo5NDo4Nzo1NyIsIm5ldE1hc2siOiIyNTUuMjU1LjI1NS4wIn0=");
//    }

    /**
     * object转String
     *
     * @param object
     * @return String
     */
    public static String toStr(Object object) {
        if (object == null) {
            return "";
        }
        return object.toString();
    }

    /**
     * 脱敏信息
     *
     * @param content 脱敏内容
     * @param start   开始位置
     * @param end     结束位置
     * @param replace 替换符号
     * @return boolean
     */
    public static String hideStringInfo(String content, int start, int end, String replace) {
        String result = "";
        StringBuffer sb = new StringBuffer();
        sb.append(content);
        if (null != sb && StringUtils.isNotBlank(sb.toString())) {
            for (int i = start; i < end; i++) {
                sb.replace(i, i + 1, replace);
            }
            result = sb.toString();
        }
        return result;
    }

    /**
     * 脱敏信息
     *
     * @param sb      脱敏内容
     * @param start   开始位置
     * @param end     结束位置
     * @param replace 替换符号
     * @return boolean
     */
    public static void tuomin(StringBuffer sb, int start, int end,
                              String replace) {
        if (null != sb && StringUtils.isNotBlank(sb.toString())) {
            for (int i = start; i < end; i++) {
                sb.replace(i, i + 1, replace);
            }
        }
    }

    public static List<String> stringToListString(String params,
                                                  String splitType) {
        List<String> results = new ArrayList<>();
        String[] param = params.split(splitType);
        for (String s : param) {
            results.add(s);
        }
        return results;
    }

    /**
     * 将json格式的字符串解析成Map对象
     */
    public static Map<String, Object> jsonStringToMap(String jsonString) {
        ObjectMapper om = new ObjectMapper();
        Map<String, Object> data = null;
        try {
            data = om.readValue(jsonString, Map.class);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 解析回传数据
     */
    public static Object stringToObjectResponse(String jdp_response, Object obj) {
        ObjectMapper om = new ObjectMapper();
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //
        Object resp = null;
        try {
            resp = om.readValue(jdp_response, obj.getClass());

        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resp;
    }

//    public static void FromBase64(String data) {
//        try {
//            String aa = new String(Base64.decodeBase64(data), "UTF-8");
//            System.out.println("====" + aa);
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * base64解密
     *
     * @param param
     * @return
     */
//    public static String BASE64Decoder(String param) {
//        String dataString = "";
//        byte[] bytes = Base64.decodeBase64(param);
//        dataString = new String(bytes);
//
//        return dataString;
//    }

    /**
     * object转String
     *
     * @param object
     * @return String
     */
    public static boolean isBlank(Object object) {
        if (CustomUtil.toStr(object).length() < 0) {
            return true;
        }
        return false;
    }

    /**
     * 时间段转每一天
     *
     * @param begintTime
     * @param endTime
     * @return
     */
    public static List<Date> findDaysDate(String begintTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = null;
        Date dEnd = null;
        try {
            dBegin = sdf.parse(begintTime);
            dEnd = sdf.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Date> daysStrList = new ArrayList<Date>();
        daysStrList.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);
        while (dEnd.after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            //String dayStr = sdf.format(calBegin.getTime());
            daysStrList.add(calBegin.getTime());
        }
        return daysStrList;
    }
}
